package com.viser.partyinrs;

import com.viser.partyinrs.MapaActivity.MyLocationListener;
import com.viser.partyinrs.adapters.PostAdapter;

import android.R.bool;
import android.app.Activity;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class PostActivity extends Activity {
	TextView txtNaziv, txtKomentar;
	Button btnSlikaj, btnPosalji;
	LocationManager locationManager;
	LocationListener locationListener;
	Location lokacija = null;
	
	
	 @Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.gde_sam_izasao);
		
		txtNaziv = (TextView)findViewById(R.id.txtNazivLokala);
		txtKomentar = (TextView)findViewById(R.id.txtKomentar);
		btnSlikaj = (Button)findViewById(R.id.btnSlikaj);
		btnPosalji = (Button)findViewById(R.id.btnPosalji);
		
		locationManager = (LocationManager)this.getSystemService(this.LOCATION_SERVICE);
		locationListener = new LocationListener() {
			
			@Override
			public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
				
			}
			
			@Override
			public void onProviderEnabled(String arg0) {
				
			}
			
			@Override
			public void onProviderDisabled(String arg0) {
				
			}
			
			@Override
			public void onLocationChanged(Location trenutnaLokacija) {
				lokacija = trenutnaLokacija;
			}
		};
		
		locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 30, 10,locationListener);
		locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 30, 10,locationListener);
		
		btnSlikaj.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// Kod za pozivanje kamere
			}
		});
		
		
		btnPosalji.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				if(unosKomentara(MainActivity.username, txtNaziv.getText().toString(), txtKomentar.getText().toString(), lokacija ))
					locationManager.removeUpdates(locationListener);
			}
		});
	}
	 
	private boolean unosKomentara(String username, String naziv, String opis, Location lokacija){
		PostAdapter adapter = new PostAdapter();
		if(adapter.dodajPost(username, naziv, opis, lokacija.getLongitude(), lokacija.getLatitude()))
			return true;
		else
			return false;
	}
	 
}
